<template>
	<view class="container">
		<view class="tab_box">
			<u-tabs :list="list" :is-scroll="false" :current="current" @change="change" active-color="#EA3D37"
				bar-height="6" bar-width="40"></u-tabs>
		</view>
		<view class="content" v-if="current !== 3">
			<view class="list" v-for="(item,index) in invoiceList" :key="index">
				<view class="left">
					<view class="order_num">订单号：{{item.order_id}}</view>
					<view class="money">金额：{{item.total_price}}</view>
					<view class="time">时间：{{item.add_time}}</view>
				</view>
				<view class="right">
					<view class="btns" @click="$gourl('/mallPage/order/orderDetails?order='+item.order_id)">查看订单</view>
					<view class="btns yellow" @click="applyClick(item.order_id)" v-if="item.status == 1">申请开票</view>
					<view class="btns" @click="lookInvoice(item.invoiceId)" v-if="item.status == 3 || item.status == 2">
						开票信息</view>
				</view>
			</view>
			<u-loadmore :status="status" v-if="status !== 'nomore'" />
		</view>
		<view class="content2" v-else>
			<!-- <view class="content2"> -->
			<view class="content3">
				<view class="list2" v-for="(item,index) in titleList" :key="index"
					@click="$gourl('/otherPage/invoice/editIdvoice?info='+JSON.stringify(item))">
					<view class="infos">
						<view class="user_info">{{item.company_name}}
						</view>
						<view class="address">纳税人识别号：{{item.identification}}</view>
					</view>
					<view class="right_box">
						<u-icon name="edit-pen" color="#333" size="40"></u-icon>
					</view>
				</view>
			</view>
			<view class="add_btn" @click="$gourl('/otherPage/invoice/addInvoice')">
				<u-icon name="plus-circle" color="#EA3D37" size="40"></u-icon>
				<text style="margin-left: 20rpx;">新增抬头</text>
			</view>
		</view>
		<u-popup v-model="infoShow" mode="center" border-radius="14">
			<view class="mask_content">
				<view class="option">
					<view>发票类型：</view>
					<view>{{invoiceinfo.type == 2?'电子发票普通发票':'电子发票专用发票'}}</view>
				</view>
				<view class="option">
					<view>单位名称：</view>
					<view>{{invoiceinfo.company_name}}</view>
				</view>
				<view class="option">
					<view>识别号：</view>
					<view>{{invoiceinfo.identification}}</view>
				</view>
				<view class="option">
					<view>手机号：</view>
					<view>{{invoiceinfo.phone}}</view>
				</view>
				<view class="option">
					<view>电子邮箱：</view>
					<view>{{invoiceinfo.email}}</view>
				</view>
				<view class="mask_btn" @click="infoShow = false">确定</view>
			</view>
		</u-popup>
		<u-popup v-model="titleShow" mode="center" border-radius="14">
			<view class="mask_content">
				<view class="option" v-for="(item,index) in titleList" :key="index" @click="select = index">
					<view class="select">
						<view class="no">
							<u-icon name="checkmark-circle-fill" color="#EA3D37" size="26"
								v-if="select == index"></u-icon>
						</view>
					</view>
					<view>{{item.company_name}}</view>
				</view>
				<view class="mask_btn" @click="applyConfirmClick">确定</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [{
					name: '未开票'
				}, {
					name: '已申请'
				}, {
					name: '已开票'
				}, {
					name: '抬头管理'
				}],
				current: 0,
				infoShow: false,
				titleShow: false,
				select: null,
				invoiceId: 0,
				order_id: 0,
				invoiceinfo: null,
				page: 1,
				pageSize: 10,
				total: 1,
				count: 1,
				invoiceList: [],
				titleList: []
			}
		},
		onLoad() {
			this.getData()
			this.getTitleList()
		},
		onShow() {
			this.titleList = []
			this.getTitleList()
		},
		onReachBottom() {
			if (this.page >= this.count) return this.status = 'nomore';
			this.status = 'loading';
			this.page = ++this.page;
			this.getData()
		},
		methods: {
			lookInvoice(info) {
				this.infoShow = true
				for (let item of this.titleList) {
					if (info == item.id) {
						this.invoiceinfo = item
					}
				}
			},
			applyConfirmClick() {
				this.$allApi.applyInvoice({
					order_id: this.order_id,
					id: this.titleList[this.select].id,
				}).then(res => {
					this.$u.toast('申请成功')
					this.titleShow = false
					this.getData()
				})
			},
			applyClick(id) {
				this.order_id = id
				this.titleShow = true
			},
			async getTitleList() {
				await this.$allApi.getInvoiceTitleList().then(res => {
					this.titleList = res.data
				})
			},
			async getData() {
				await this.$allApi.getInvoiceList({
					page: this.page,
					limit: this.pageSize,
					type: this.current
				}).then(res => {
					if (this.page == 1) {
						this.invoiceList = res.data
					} else {
						this.couponList = [...this.couponList, ...res.data]
					}
					if (res.data.length < this.pageSize) {
						this.status = 'nomore'
					} else {
						this.status = 'loadmore'
					}
				})
			},
			change(index) {
				this.current = index;
				this.invoiceList = []
				this.titleList = []
				if (index !== 3) {
					this.page = 1
					this.getData()
				} else {
					this.getTitleList()
				}
			},
		}
	}
</script>

<style scoped lang="scss">
	.mask_content {
		padding: 30rpx;
		padding-top: 0;
		width: 710rpx;

		.mask_btn {
			width: 314rpx;
			height: 80rpx;
			border-radius: 10rpx;
			background: #EA3D37;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #fff;
			margin: 0 auto;
			margin-top: 40rpx;
		}

		.option {
			height: 120rpx;
			display: flex;
			align-items: center;
			justify-content: flex-start;
			border-bottom: 1px solid #ECECEC;

			.select {
				margin-right: 20rpx;

				.no {
					width: 34rpx;
					height: 34rpx;
					border-radius: 50%;
					border: 1px solid #EA3D37;
					display: flex;
					align-items: center;
					justify-content: center;
				}
			}
		}
	}

	.content3 {
		// background: #fff;
		// min-height: calc(100vh - 140rpx);
		// padding-bottom: calc(90rpx + env(safe-area-inset-bottom));
		padding: 20rpx;

		.list2 {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 30rpx;
			background: #fff;
			margin-bottom: 20rpx;
			line-height: 2;

			.right_box {
				width: 100rpx;
				height: 40rpx;
				display: flex;
				align-items: center;
				justify-content: flex-end;
			}

			.select_box {
				width: 100rpx;
				height: 40rpx;
				border-radius: 8rpx;
				background: #eaf4eb;
				border: 2rpx solid #1A8A00;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #1A8A00;

			}

			.infos {
				flex: 1;

				.user_info {
					font-size: 30rpx;
					font-weight: 400;
					display: flex;
					justify-content: flex-start;
					align-items: center;

					view {
						width: 74rpx;
						height: 30rpx;
						border-radius: 30rpx;
						margin-left: 20rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						font-size: 20rpx;
						color: #e2b771;
						background: linear-gradient(to right, #585858, #333);
						;
					}
				}

				.address {
					font-size: 24rpx;
					color: #333;
				}
			}
		}
	}

	.add_btn {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100vw;
		height: 120rpx;
		padding-bottom: calc(-30rpx + env(safe-area-inset-bottom));
		display: flex;
		align-items: center;
		justify-content: center;
		border-top: 1px solid #eee;
	}

	.list {
		width: 710rpx;
		height: 226rpx;
		padding: 20rpx 30rpx;
		background: #FFFFFF;
		border-radius: 10rpx;
		margin-bottom: 20rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.left {
			flex: 1;
			line-height: 2;
		}

		.right {
			.btns {
				width: 160rpx;
				height: 52rpx;
				border-radius: 6rpx;
				background: #EAF4E8;
				border: 2rpx solid #EA3D37;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #EA3D37;
				margin-bottom: 30rpx;
			}

			.yellow {
				background: #f9fade;
				border: 2rpx solid #F8CF00;
				color: #F8CF00;
			}

			.btns:last-child {
				margin-bottom: 0;
			}
		}
	}

	.container {
		min-height: 100vh;
		background: #F8F8F8;
	}

	.content {
		padding: 20rpx;
	}

	.content2 {
		padding: 20rpx;
		// background: #fff;
	}

	.tab_box {
		width: 750rpx;
		height: 100rpx;
		background: #FFFFFF;
		border-bottom: 1px solid #f0f0f0;
		position: sticky;
		left: 0;
		top: 0;
	}
</style>